<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="库存商品及原材料" prop="commodityName">
        <el-input
          v-model="queryParams.commodityName"
          placeholder="请输入库存商品及原材料"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签收量" prop="signInNum">
        <el-input
          v-model="queryParams.signInNum"
          placeholder="请输入签收量/吨"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消耗" prop="consumption">
        <el-input
          v-model="queryParams.consumption"
          placeholder="请输入消耗（数量）/吨"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人姓名" prop="createByName">
        <el-input
          v-model="queryParams.createByName"
          placeholder="请输入创建人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="创建人部门" prop="createByDept">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.createByDept"-->
      <!--          placeholder="请输入创建人部门"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="创建人部门" prop="createByDeptId" v-hasPermi="['data_upload:uhpc_stock:list_all']">
        <el-select
          v-model="queryParams.createByDeptId"
          placeholder="请选择创建人部门"
          clearable
          @visible-change="getDepartments"
        >
          <el-option
            v-for="dept in deptList"
            :key="dept.deptId"
            :label="dept.deptName"
            :value="dept.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="创建人id" prop="createById">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.createById"-->
      <!--          placeholder="请输入创建人id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <br/>
      <el-form-item>
        <el-button
          v-hasPermi="['data_upload:uhpc_stock:list_all','data_upload:uhpc_stock:list','data_upload:uhpc_stock:list_by_dept_id']"
          type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
        >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['data_upload:uhpc_stock:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <!--        <el-button-->
        <!--          type="success"-->
        <!--          plain-->
        <!--          icon="el-icon-edit"-->
        <!--          size="mini"-->
        <!--          :disabled="single"-->
        <!--          @click="handleUpdate"-->
        <!--          v-hasPermi="['data_upload:uhpc_stock:edit']"-->
        <!--        >修改-->
        <!--        </el-button>-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['data_upload:uhpc_stock:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['data_upload:uhpc_stock:export']"
        >导出
        </el-button>
        <el-button
          type="success"
          plain
          size="mini"
          @click="changeTable"
          v-hasPermi="['data_upload:uhpc_stock:change']"
        >切换
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="uhpc_stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="id" align="center" prop="commodityId"/>-->
      <el-table-column v-if="!isChange" label="库存商品及原材料" align="center" prop="commodityName"/>
      <el-table-column v-if="!isChange" label="签收量/吨" align="center" prop="signInNum"/>
      <el-table-column v-if="!isChange" label="消耗（数量）/吨" align="center" prop="consumption"/>
      <el-table-column label="创建人姓名" align="center" prop="createByName"/>
      <el-table-column label="创建人部门" align="center" prop="createByDept"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="创建人部门id" align="center" prop="createByDeptId"/>-->
      <!--      <el-table-column label="创建人id" align="center" prop="createById"/>-->
      <el-table-column label="备注" align="center" prop="remark"/>
      <!--      <el-table-column label="状态标志" align="center" prop="status"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="!isChange"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['data_upload:uhpc_stock:remove']"
          >删除
          </el-button>
          <el-button
            v-if="isChange"
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="getOneBatchInfoByRemark(scope.row.remark)"
            v-hasPermi="['data_upload:uhpc_stock:info']"
          >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改UHPC（库存）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="521px" append-to-body>
      <!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
      <!--        <el-form-item label="库存商品及原材料" prop="commodityName">-->
      <!--          <el-input v-model="form.commodityName" placeholder="请输入库存商品及原材料"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="签收量/吨" prop="signInNum">-->
      <!--          <el-input v-model="form.signInNum" placeholder="请输入签收量/吨"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="消耗" prop="consumption">-->
      <!--          <el-input v-model="form.consumption" placeholder="请输入消耗"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="创建人姓名" prop="createByName">-->
      <!--          <el-input v-model="form.createByName" placeholder="请输入创建人姓名"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="创建人部门" prop="createByDept">-->
      <!--          <el-input v-model="form.createByDept" placeholder="请输入创建人部门"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="创建人部门id" prop="createByDeptId">-->
      <!--          <el-input v-model="form.createByDeptId" placeholder="请输入创建人部门id"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="创建人id" prop="createById">-->
      <!--          <el-input v-model="form.createById" placeholder="请输入创建人id"/>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="备注" prop="remark">-->
      <!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>-->
      <!--        </el-form-item>-->
      <!--      </el-form>-->

      <el-table
        style="width: 100%"
        :data="commodities"
        border
      >
        <el-table-column label="库存商品及原材料" prop="commodityName" width="200" align="center">
        </el-table-column>
        <el-table-column label="签收量/吨" prop="signInNum" width="140" align="center">
          <template slot-scope="scope">
            <el-input style="width: 100%;height: 100%" v-model="scope.row.signInNum"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="消耗（数量）/吨" prop="consumption" width="140" align="center">
          <template slot-scope="scope">
            <el-input style="width: 100%;height: 100%" v-model="scope.row.consumption"></el-input>
          </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="batchAdd" :disabled="isDisabled">确 定</el-button>
        <el-button @click="cancel" :disabled="isDisabled">取 消</el-button>
      </div>
    </el-dialog>


    <!--  详情显示-->
    <el-dialog :title="title" :visible.sync="oneBatchInfoOpen" width="521px" append-to-body>
      <div v-if="oneBatchInfo.length!==0">
        <el-tag style="margin-right: 10px">{{ oneBatchInfo[0].createByName }}</el-tag>
        <el-tag style="margin-right: 10px">{{ oneBatchInfo[0].createByDept }}</el-tag>
        <el-tag style="margin-right: 10px">{{ parseTime(oneBatchInfo[0].createTime) }}</el-tag>
      </div>
      <br/>
      <el-table
        style="width: 100%"
        :data="oneBatchInfo"
        border
      >
        <el-table-column label="库存商品及原材料" prop="commodityName" width="200" align="center">
        </el-table-column>
        <el-table-column label="签收量/吨" prop="signInNum" width="140" align="center">
        </el-table-column>
        <el-table-column label="消耗（数量）/吨" prop="consumption" width="140" align="center">
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button @click="closeOneBatchInfo">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUhpc_stock, getUhpc_stock, delUhpc_stock, addUhpc_stock, updateUhpc_stock } from "@/api/data_upload/uhpc_stock";
import { listDept } from '@/api/system/dept'
import { nanoid } from 'nanoid'

export default {
  name: 'Uhpc_stock',
  data() {
    return {
      isChange:true,
      oneBatchInfo: [],
      oneBatchInfoOpen: false,
      commodities: [
        {
          commodityId: null,
          commodityName: '预混料',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }, {
          commodityId: null,
          commodityName: '核心料',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }, {
          commodityId: null,
          commodityName: '预混粉料',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }, {
          commodityId: null,
          commodityName: '钢纤维',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }, {
          commodityId: null,
          commodityName: '外加剂',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        },
        {
          commodityId: null,
          commodityName: '配砂',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        },
        {
          commodityId: null,
          commodityName: '其他',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }
      ],
      isDisabled: false,
      // 状态标志时间范围
      daterangeCreateTime: [],
      deptList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // UHPC（库存）表格数据
      uhpc_stockList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commodityName: null,
        signInNum: null,
        consumption: null,
        createByName: null,
        createByDept: null,
        createByDeptId: null,
        createById: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    changeTable() {
      this.isChange = !this.isChange
      this.resetQuery()
      this.getList()
    },
    closeOneBatchInfo() {
      this.oneBatchInfoOpen = false
      this.oneBatchInfo = []
    },
    getOneBatchInfoByRemark(remark) {
      getUhpc_stock(remark).then(response => {
        this.oneBatchInfo = response.data
        this.oneBatchInfoOpen = true
      }).catch(() => {
        this.$modal.msgError('获取失败')
      })

    },
    getDepartments() {
      if (this.deptList == null || this.deptList.length === 0) {
        listDept({}).then(response => {
          this.deptList = response.data
        })
      }
      return true
    },
    /** 查询UHPC（库存）列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      this.queryParams.isChange = this.isChange
      if (null != this.daterangeCreateTime && '' !== this.daterangeCreateTime) {
        this.queryParams.params['beginCreateTime'] = this.daterangeCreateTime[0]
        this.queryParams.params['endCreateTime'] = this.daterangeCreateTime[1]
      }
      listUhpc_stock(this.queryParams).then(response => {
        this.uhpc_stockList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        commodityId: null,
        commodityName: null,
        signInNum: null,
        consumption: null,
        createTime: null,
        createByName: null,
        createByDept: null,
        createByDeptId: null,
        createById: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        status: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commodityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加UHPC（库存）'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const commodityId = row.commodityId || this.ids
      getUhpc_stock(commodityId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改UHPC（库存）'
      })
    },

    batchAdd() {
      this.$modal.msg('正在添加...')
      this.isDisabled = true

      new Promise((resolve, reject) => {
        let nanoId = nanoid()
        for (let i = 0; i < this.commodities.length-1; i++) {
          this.commodities[i].remark = nanoId
          addUhpc_stock(this.commodities[i])
        }
        this.commodities[this.commodities.length-1].remark = nanoId
        addUhpc_stock(this.commodities[this.commodities.length-1]).then(()=>{
          this.getList()
        })
        resolve()
      }).then(() => {
        this.$modal.msgSuccess('新增成功')
        this.resetCommodities()
        this.open = false
        this.isDisabled = false
      }).catch(() => {
        this.$modal.msgSuccess('新增失败')
        this.open = false
        this.isDisabled = false
      })

    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.commodityId != null) {
            updateUhpc_stock(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addUhpc_stock(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commodityIds = row.commodityId || this.ids
      this.$modal.confirm('是否确认删除UHPC（库存）编号为"' + commodityIds + '"的数据项？').then(function() {
        return delUhpc_stock(commodityIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('data_upload/uhpc_stock/export', {
        ...this.queryParams
      }, `uhpc_stock${new Date().getTime()}.xlsx`)
    },
    resetCommodities(){
      this.commodities=[
        {
          commodityId: null,
          commodityName: '预混料',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }, {
          commodityId: null,
          commodityName: '核心料',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }, {
          commodityId: null,
          commodityName: '预混粉料',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }, {
          commodityId: null,
          commodityName: '钢纤维',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }, {
          commodityId: null,
          commodityName: '外加剂',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        },
        {
          commodityId: null,
          commodityName: '配砂',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        },
        {
          commodityId: null,
          commodityName: '其他',
          signInNum: null,
          consumption: null,
          createTime: null,
          createByName: null,
          createByDept: null,
          createByDeptId: null,
          createById: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          status: null
        }
      ]
    }
  }
}
</script>
